package com.health.mapp.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.health.db.model.UserHospitalRecord;
import com.health.db.model.UserHospitalRecordExample;
import com.health.db.model.UserHospitalRecordTemp;
import com.health.db.service.user.IUserHospitalRecordService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.UserHospitalRecordReq;
import com.health.mapp.bean.UserHospitalRecordResp;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;
/**

 *健康档案
 */

public class UserHospitalRecordImpl implements ActionInterface {

	
	private IUserHospitalRecordService userHospitalRecordService  ;
	
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
	 

		UserHospitalRecordReq req=(UserHospitalRecordReq) reqBean;
		UserHospitalRecordResp _resp= new UserHospitalRecordResp();
		
		String method = req.getMethod();
		 		
		 //医生编号 用户编号 订单号 取一
		 if(StringUtils.isNotBlank(method)){

			 //查询
			 if (method.equals("2")||method.equals("5")) {
				 return  read(req, _resp);
				
			 //更新
			 }else if (method.equals("1")) {
				 return  create(req, _resp);
				
			}else if (method.equals("3")){
				 return  update(req, _resp);
				
			}else if (method.equals("4")){
				 return  delete(req, _resp);
				
			}else{
				_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
				_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
				return _resp;
			}
			 
			 
		}else {
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}
	}
	
	/**
	 * 读取操作
	 * @param req
	 * @param _resp
	 * @return
	 */
	private UserHospitalRecordResp read(UserHospitalRecordReq req,UserHospitalRecordResp _resp){
		
		 //单条详情查询
		 if (StringUtils.isNotBlank(req.getRecId())) {
		
			 UserHospitalRecordTemp userHospitalRecordTemp = new UserHospitalRecordTemp();
			 
			 UserHospitalRecord userHospitalRecord = userHospitalRecordService.selectByPrimaryKey(Long.valueOf(req.getRecId()));
			 
			 userHospitalRecordTemp = this.copyProperties(userHospitalRecord,new UserHospitalRecordTemp());
			 //userHospitalRecordTemp.setImgUrlList(userHospitalRecord.getUserHospitalRecordList());
			 _resp.setUserHospitalRecord(userHospitalRecordTemp);
			 return _resp;
			 
		 //列表查询	 
		 }else if (StringUtils.isNotBlank(req.getUserId())&&StringUtils.isNotBlank(req.getPatientId())) {
			 
			 //参数
			 UserHospitalRecordExample temp = new UserHospitalRecordExample();
				
				Integer pageNo=req.getPageNo();
				Integer pageSize=req.getPageNum();
				
				if(null!=pageNo&&null!=pageSize){
					temp.setOffset((pageNo.intValue()-1)*pageSize.intValue());
					temp.setPageSize(pageSize.intValue());
					_resp.setPageNo(pageNo);
					_resp.setPageNum(pageSize);
					 
				}
				
				//提供按照宝宝信息查询
				if (StringUtils.isNoneBlank(req.getPatientId())) {
					temp.setPatientId(Long.valueOf(req.getPatientId()));
				}
				
				temp.setUserId(Long.valueOf(req.getUserId()));
				
				List< UserHospitalRecordTemp> UserHospitalRecordTempList  = new ArrayList<UserHospitalRecordTemp>();
				
				List< UserHospitalRecord> userHospitalRecordList = userHospitalRecordService.selectByExample(temp);
				
				for (int i = 0; i < userHospitalRecordList.size(); i++) {
					UserHospitalRecordTemp  userHospitalRecordTemp = new UserHospitalRecordTemp();
					UserHospitalRecordTempList.add(this.copyProperties(userHospitalRecordList.get(i), userHospitalRecordTemp));
				}
				
				_resp.setUserHospitalRecordList(UserHospitalRecordTempList);
				 return _resp;
			
		}else{
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}
	}
	
	private UserHospitalRecordTemp copyProperties(UserHospitalRecord userHospitalRecord,UserHospitalRecordTemp userHospitalRecordTemp){
		
		userHospitalRecordTemp.setRecId(String.valueOf(userHospitalRecord.getRecId().intValue()));
		userHospitalRecordTemp.setUserId(String.valueOf(userHospitalRecord.getUserId().intValue()));
		userHospitalRecordTemp.setPatientId(null==userHospitalRecord.getPatientId()?"":String.valueOf(userHospitalRecord.getPatientId()));
		
		userHospitalRecordTemp.setPatientName(userHospitalRecord.getPatientName());
		userHospitalRecordTemp.setTreatTime(null==userHospitalRecord.getTreatTime()?"":DateUtils.formatDate(userHospitalRecord.getTreatTime()));
		userHospitalRecordTemp.setHosptitalName(userHospitalRecord.getHosptitalName());
		userHospitalRecordTemp.setIllnessName(userHospitalRecord.getIllnessName());
		userHospitalRecordTemp.setIllnessDesc(userHospitalRecord.getIllnessDesc());
		userHospitalRecordTemp.setCreateTime(null==userHospitalRecord.getCreateTime()?"":DateUtils.formatDatetime(userHospitalRecord.getCreateTime()));
		userHospitalRecordTemp.setImgUrls(userHospitalRecord.getImgUrls());
		
		return userHospitalRecordTemp;
	}

	/**
	 * 新增操作
	 * @param req
	 * @param _resp
	 * @return
	 */
	private UserHospitalRecordResp create(UserHospitalRecordReq req,UserHospitalRecordResp _resp){
		
		if (StringUtils.isNotBlank(req.getUserId())) {
			UserHospitalRecordExample record = new UserHospitalRecordExample() ;
			record.setUserId(Long.valueOf(req.getUserId()));
			record.setPatientId(StringUtils.isBlank(req.getPatientId())?null:Long.valueOf(req.getPatientId()));
			record.setPatientName(req.getPatientName());
			record.setHosptitalName(req.getHosptitalName());
			record.setIllnessName(StringUtils.isBlank(req.getIllnessName())?"":req.getIllnessName());
			record.setIllnessDesc(StringUtils.isBlank(req.getIllnessDesc())?"":req.getIllnessDesc());
			
			try {
				record.setTreatTime(StringUtils.isBlank(req.getTreatTime())?null:DateUtils.parseDate(req.getTreatTime()));
			} catch (Exception e) {
				record.setTreatTime(null);
			}
		
			record.setImgUrls(req.getImgUrls());
			userHospitalRecordService.insert(record);
			return _resp;
			
		}else{
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}
		
	
	}
	
	
	/**
	 * 更新操作
	 * @param req
	 * @param _resp
	 * @return
	 */
	private UserHospitalRecordResp update(UserHospitalRecordReq req,UserHospitalRecordResp _resp){
		
		if (StringUtils.isNotBlank(req.getRecId())) {
			UserHospitalRecord currRecord= userHospitalRecordService.selectByPrimaryKey(Long.valueOf(req.getRecId()));
			
			UserHospitalRecordExample record = new UserHospitalRecordExample();
			record.setRecId(Long.valueOf(req.getRecId()));
			record.setUserId(currRecord.getUserId());
			record.setPatientId(StringUtils.isBlank(req.getPatientId())?null:Long.valueOf(req.getPatientId()));
			record.setPatientName(req.getPatientName());
			record.setHosptitalName(req.getHosptitalName());
			record.setIllnessName(StringUtils.isBlank(req.getIllnessName())?"":req.getIllnessName());
			record.setIllnessDesc(StringUtils.isBlank(req.getIllnessDesc())?"":req.getIllnessDesc());
			try {
				record.setTreatTime(StringUtils.isBlank(req.getTreatTime())?null:DateUtils.parseDate(req.getTreatTime()));
			} catch (Exception e) {
				record.setTreatTime(null);
				// TODO: handle exception
			}
		
			record.setImgUrls(req.getImgUrls());
			int executNum =userHospitalRecordService.updateByPrimaryKeySelective(record);
			
			if (executNum!=1) {
				_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3031);
				_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3031_NOTE);
				return _resp;
			}
			return _resp;
			
		}else{
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}
	}
	/**
	 * 删除操作
	 * @param req
	 * @param _resp
	 * @return
	 */
	private UserHospitalRecordResp delete(UserHospitalRecordReq req,UserHospitalRecordResp _resp){
		if (StringUtils.isNotBlank(req.getRecId())) {
			

			int executNum= userHospitalRecordService.deleteByPrimaryKey(Long.valueOf(req.getRecId()));
			if (executNum!=1) {
				_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3030);
				_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3030_NOTE);
				return _resp;
			}
			
			return _resp;
			
		}else{
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}
	}
	

	public IUserHospitalRecordService getUserHospitalRecordService() {
		return userHospitalRecordService;
	}


	public void setUserHospitalRecordService(
			IUserHospitalRecordService userHospitalRecordService) {
		this.userHospitalRecordService = userHospitalRecordService;
	}

}
